<?
/**
 * Copyright 2008 Melange.
 *
 * This file is part of Melange CMS
 *
 * @package cms
 *
 */


/**
 * Requirements
 */
require_once("mvc/action/ActionForm.php");
require_once("mvc/action/ActionMessage.php");
require_once("mvc/action/ActionMessages.php");
require_once("cms/user/om/User.php");



/**
 * BaseUserForm Class
 *
 * @category    Melange CMS
 * @package     cms
 * @copyright   Copyright (c) 2007 Jeroen Simons. All rights reserved
 * @author      Jeroen Simons <jeroen@melange.nl>
 * @link        http://www.melange.nl/
 */
class BaseUserForm extends ActionForm {


    // ----------------------------------------------------- Instance Variables


    /**
     * user
     *
     * @var User 
     */
    protected $user;


    /**
     * password2
     *
     * @var mixed 
     */
    protected $password2;


    /**
     * roles
     *
     * @var mixed 
     */
    protected $roles;


    // ----------------------------------------------------------- Constructors


    /**
     * Construct new BaseUserForm
     */
    public function __construct() {
        $this->user = new User();
        $this->password2 = null;
        $this->roles = array();
    }


    // ------------------------------------------------------------- Properties


    /**
     * Get User
     *
     * @return User 
     */
    public function getUser() {
        return $this->user;
    }


    /**
     * Set User
     *
     * @param User  user
     */
    public function setUser(User $user) {
        $this->user = $user;
    }


    /**
     * Get Password2
     *
     * @return mixed 
     */
    public function getPassword2() {
        return $this->password2;
    }


    /**
     * Set Password2
     *
     * @param mixed  password2
     */
    public function setPassword2($password2) {
        $this->password2 = $password2;
    }


    /**
     * Get Roles
     *
     * @return mixed 
     */
    public function getRoles() {
        return $this->roles;
    }


    /**
     * Set Roles
     *
     * @param mixed  roles
     */
    public function setRoles($roles) {
        $this->roles = $roles;
    }


    // --------------------------------------------------------- Public Methods


    /**
     * Validate this form
     *
     * @return array $errors
     */
    public function validate() {

        $errors = new ActionMessages();

        if(is_null($this->getUser()->getUsername()) || strlen($this->getUser()->getUsername()) == 0)
            $errors->add("user->username",
                new ActionMessage("errors.required", true, new ActionMessage("prompt.username", true)));


        if(!is_null($this->getUser()->getUsername()) && strlen($this->getUser()->getUsername()) > 0 && strlen($this->getUser()->getUsername()) < 3)
            $errors->add("user->username",
                new ActionMessage("errors.minlength", true, array(
                    new ActionMessage("prompt.username", true),
                    new ActionMessage("3", false))));

        if(!is_null($this->getUser()->getUsername()) && strlen($this->getUser()->getUsername()) > 0 && strlen($this->getUser()->getUsername()) > 32)
            $errors->add("user->username",
                new ActionMessage("errors.maxlength", true, array(
                    new ActionMessage("prompt.username", true),
                    new ActionMessage("32", false))));


        if(!is_null($this->getUser()->getPassword()) && strlen($this->getUser()->getPassword()) > 0 && strlen($this->getUser()->getPassword()) < 3)
            $errors->add("user->password",
                new ActionMessage("errors.minlength", true, array(
                    new ActionMessage("prompt.password", true),
                    new ActionMessage("3", false))));

        if(!is_null($this->getUser()->getPassword()) && strlen($this->getUser()->getPassword()) > 0 && strlen($this->getUser()->getPassword()) > 32)
            $errors->add("user->password",
                new ActionMessage("errors.maxlength", true, array(
                    new ActionMessage("prompt.password", true),
                    new ActionMessage("32", false))));


        return $errors;
    }

}
?>